package com.dtm.adapter;

import java.util.List;

import javax.jmdns.ServiceInfo;

import com.dtm.R;
import com.dtm.client.ConvertPackge;
import com.dtm.model.User;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ServerAdapter extends ArrayAdapter<ServiceInfo> {
	private LayoutInflater inflater;
	
	public ServerAdapter(Context context, int resource,
			List<ServiceInfo> objects) {
		super(context, resource, objects);
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ServiceInfo item = getItem(position);
		if (null == convertView) {
			convertView = inflater.inflate(R.layout.item_server, null);
		}
		TextView txtHostName = (TextView) convertView.findViewById(R.id.txt_host_name);
		TextView txtHostIP = (TextView) convertView.findViewById(R.id.txt_host_ip);
		ImageView avatar = (ImageView) convertView.findViewById(R.id.ibtn_avatar);
		avatar.setImageResource(User.getImage(ConvertPackge.getCode(item.getName())));
		txtHostName.setText(item.getName());
		txtHostIP.setText("IP : " + item.getInet4Addresses()[0].toString());
		return convertView;
	}

}
